//
//  ListViewController.m
//  imageSet
//
//  Created by sunkai on 2017/7/21.
//  Copyright © 2017年 sunkai. All rights reserved.
//

#import "ListViewController.h"
#import "LLNetWork.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
@interface ListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView *mCollection;
@property (nonatomic,assign)BOOL isLoadData;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _titleStr;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createCollectionView];
    
}
#pragma mark collectionview
- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _mCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:flowLayout];
    _mCollection.delegate = self;
    _mCollection.dataSource = self;
    [_mCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCellID"];
    [self.view addSubview:_mCollection];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _listArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCellID" forIndexPath:indexPath];
    CGFloat imageWidth = (SCREEN_WIDTH-40)/3;

    UIImageView *headerImage = [cell.contentView viewWithTag:101];
    if (!headerImage) {
        headerImage = [[UIImageView alloc]init];
        headerImage.frame = CGRectMake(0, 0, imageWidth, imageWidth*1.264);
        headerImage.tag = 101;
        [cell.contentView addSubview:headerImage];
    }
    [headerImage sd_setImageWithURL:[NSURL URLWithString:_listArray[indexPath.row][@"HeadPic"]]];
    UILabel *headerLabel = [cell.contentView viewWithTag:102];
    if (!headerLabel) {
        headerLabel = [[UILabel alloc]init];
        headerLabel.frame = CGRectMake(0, imageWidth*1.264+5, imageWidth, 50);
        headerLabel.textColor = [UIColor whiteColor];
        headerLabel.tag = 102;
        headerLabel.numberOfLines = 0;
        headerLabel.font = [UIFont systemFontOfSize:12];
        [cell.contentView addSubview:headerLabel];
    }
    headerLabel.text = _listArray[indexPath.row][@"JieTitle"];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self showImageWithIndexPath:indexPath];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat imageWidth = (SCREEN_WIDTH-40)/3;
    return CGSizeMake(imageWidth, imageWidth*1.264+5+50);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
#pragma mark 展示图片
- (void)showImageWithIndexPath:(NSIndexPath *)indexPath {
    if (_isLoadData) {
        return;
    }
    _isLoadData = YES;
    NSString *urlStr = [NSString stringWithFormat:@"http://api.kongyouran.com/galPhoto/?id=%@",_listArray[indexPath.row][@"JieID"]];
    [LLNetWork getDataWithURLString:urlStr callBack:^(id responseObject, NSError *error) {
        NSArray *bigImageArray = [NSArray array];
        bigImageArray = responseObject[@"ListContent"];
        if (bigImageArray.count == 0) {
            return ;
        }
        NSMutableArray *photos = [NSMutableArray new];
        for (int i = 0; i < bigImageArray.count; i ++) {
            MJPhoto *photo = [[MJPhoto alloc] init];
            photo.url = [NSURL URLWithString:[bigImageArray objectAtIndex:i]]; // 图片路径
            [photos addObject:photo];
        }
        
        MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
        browser.currentPhotoIndex = 0;
        browser.photos = photos;
        [browser show];
        LLDispatch_after(0.2, ^{
            _isLoadData = NO;
        });
    }];
}

@end
